import { reactive } from "vue";
import { getById } from "@/api/recipe";

const uploadServerUrl = `${import.meta.env.VITE_API_SERVER}`;

export const _$ = reactive({
  state: {
    previewDialogVisible: false,
    dialogImageUrl: "",
  },
  requestParam: {
    id: null,
  },
  fileList: [],
  responseParam: {},
});

export const $ = {
  loadDetail() {
    getById(_$.requestParam.id).then((data) => {
      _$.responseParam = data;
      _$.responseParam.images.split(",").forEach((item) => {
        _$.fileList.push({
          url: `${uploadServerUrl}${item}?Authorization=Bearer ${localStorage.getItem(
            "ats-user-token"
          )}`,
        });
      });
    });
  },

  onPreview(uploadFile) {
    _$.state.dialogImageUrl = uploadFile.url;
    _$.state.previewDialogVisible = true;
  },

  clean() {
    _$.responseParam = {};
    _$.fileList.length = 0;
  },
};

export const to = {};
